package redis

import (
	"context"
	"time"

	"github.com/go-redis/redis/v8"
)

var RDB *redis.Client

var ctx = context.Background()

func RedisInit() {
	RDB = redis.NewClient(&redis.Options{
		Addr:     "linux.server:6379",
		Password: "",
		DB:       0,
	})
}

// 增加超时时间
func SetKey(key string, value string, seconds int) bool {
	if seconds > 0 {
		err := RDB.Set(ctx, key, value, time.Duration(seconds)*time.Second).Err()
		return err == nil
	} else {
		err := RDB.Set(ctx, key, value, 0).Err()
		return err == nil
	}
}

func GetKey(key string) string {
	val, err := RDB.Get(ctx, key).Result()
	if err != nil {
		return ""
	}
	return val
}

func HSet(item string, key string, value string) bool {
	err := RDB.HSet(ctx, item, key, value).Err()
	return err == nil
}

func HGet(item string, key string) string {
	val, err := RDB.HGet(ctx, item, key).Result()
	if err != nil {
		return ""
	}
	return val
}
